// 将指定元素平滑滚动到浏览器窗口的可见区域
const smoothScroll = element =>
	document.querySelector(element).scrollIntoView({
		behavior: 'smooth'
	});

// 判断obj对象是否为空
const isEmptyObject = (obj) => {
	if (!obj || typeof obj !== 'object' || Array.isArray(obj))
		return false
	return !Object.keys(obj).length
}

// 随机生成颜色
const randomColor = () => {
	return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
}

// 递归生成树形结构
const getTreeData = (data, pid, pidName = 'parentId', idName = 'id', childrenName = 'children', key) => {
	let arr = [];

	for (let i = 0; i < data.length; i++) {
		if (data[i][pidName] == pid) {
			data[i].key = data[i][idName];
			data[i][childrenName] = getTreeData(data, data[i][idName], pidName, idName, childrenName);
			arr.push(data[i]);
		}
	}

	return arr;

}

// 根据pid生成树形结构
const createTreeByPid = (items, id = null, link = 'pid') => {
	items.filter(item => item[link] === id).map(item => ({
		...item,
		children: createTree(items, item.id)
	}));
};

// 生成指定范围的随机整数
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

// 数字超过上限自动加上+
const outOfNum = (val, maxNum) => {
	val = val ? val - 0 : 0;
	if (val > maxNum) {
		return `${maxNum}+`
	} else {
		return val;
	}
};


export {
	outOfNum,
	randomIntegerInRange,
	smoothScroll,
	isEmptyObject,
	randomColor,
	getTreeData
}